We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

$element->setLabel() and css class

How do I add a css "class=whatever" to the Label?

In a Form class:

<?php
MyForm extends Form(
    public function initialize() {
        $email = new Text('email');
        $email->setLabel('email')
        // How do I add a css "class=whatever" to the above email label?
        //...
        $this->add($email);     
    }
);


5.2k
Accepted
answer

https://docs.phalcon.io/en/latest/reference/forms.html

Hardcode like this

<form method="post">
    <?php
        // Проходим через форму
        foreach ($form as $element) {

            // Собираем все сгенерированные сообщения для текущего элемента
            $messages = $form->getMessagesFor($element->getName());

            if (count($messages)) {
                // Выводим каждый элемент
                echo '<div class="messages">';
                foreach ($messages as $message) {
                    echo $message;
                }
                echo '</div>';
            }

            echo '<p>';
            echo '<label for="', $element->getName(), '">', $element->getLabel(), '</label>';
            echo $element;
            echo '</p>';

        }
    ?>
    <input type="submit" value="Send"/>
</form>

Or you can try to create own \Phalcon\Tag, pass it to Di and rewrite lable method maybe it can helps you



12.2k

Yes, I know about this, thank you. I thought there is an array $option or something which I am missing, but seems not. Will make my own Tag then.



12.2k

After checking the code, it actually can be done, pass the array with options to the label().

In Volt:

<?php
form.label('email', ['class': 'whatever']) 

or in PHP

<?php
$form->label('email', ['class' => 'whatever']);

Yeap, but we was talking about Form Elements as I known.